//
//  JXHomeViewController.m
//  JXAddressApp
//
//  Created by huawei.zheng on 2019/1/22.
//  Copyright © 2019 zhhw_home. All rights reserved.
//

#import "JXHomeViewController.h"
#import "QMUISearchBar.h"
#import <QMUIKit/QMUICommonDefines.h>
#import "EgovaNetworking.h"
#import "JXAddressDetailViewController.h"

@interface JXHomeViewController ()
<
UISearchBarDelegate,
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic, strong) QMUISearchBar *searchBar;
@property (nonatomic, strong) UITableView   *resultTableView;

@property (nonatomic, strong) NSArray       *resultSets;

@end

@implementation JXHomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    _searchBar = [[QMUISearchBar alloc] initWithFrame:CGRectMake(0,IS_NOTCHED_SCREEN?40:20, SCREEN_WIDTH, 44)];
    _searchBar.delegate = self;
    _searchBar.backgroundColor = [UIColor whiteColor];
    if(@available(iOS 11.0, *)) {
        [[_searchBar.heightAnchor constraintEqualToConstant:44] setActive:YES];
    }
    
    
    _resultTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _resultTableView.tableFooterView = [UIView new];
    _resultTableView.delegate = self;
    _resultTableView.dataSource = self;
    _resultTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_resultTableView];
    
    if (@available(iOS 11.0, *)) {
        _resultTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    CGFloat height = SCREEN_HEIGHT- (IS_NOTCHED_SCREEN?88:64) - (IS_NOTCHED_SCREEN?88:48);
    _resultTableView.frame = CGRectMake(0, (IS_NOTCHED_SCREEN?88:64), SCREEN_WIDTH, height);
    
    
    [self.view addSubview:_searchBar];
    [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"取消";
    
    for (UIView *subview in _searchBar.subviews) {
        for(UIView* grandSonView in subview.subviews){
            if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                grandSonView.alpha = 0.0f;
            }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
                NSLog(@"Keep textfiedld bkg color");
            }else{
                grandSonView.alpha = 0.0f;
            }
        }
    }
    
    [self search:@""];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

#pragma mark - UITalbeView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _resultSets.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainSearchCell"];
    NSDictionary *item = _resultSets[indexPath.row];
    
    cell.textLabel.text = item[@"name"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = [_resultSets objectAtIndex:indexPath.row];
    
    [self.searchBar resignFirstResponder];
    
    [self.searchBar setText:item[@"name"]];
    
    JXAddressDetailViewController *vc = [[JXAddressDetailViewController alloc] init];
    vc.info = item;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)search:(NSString*)keyword
{
    NSDictionary *params = @{@"q":keyword?:@"",
                             @"type":@(-1),
                             @"limit":@(10)
                             };
    
    NSString *url = @"http://www.wegis.net/demo/home/baseinfo/getplacebyname";
    [EgovaNetworking postWithUrl:url cache:NO params:params completed:^(NSInteger code, id data, NSString *msg) {
        self.resultSets = data[@"features"];
        
        [self.resultTableView reloadData];
    }];
}


@end
